package com.ruoyi.notice.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 23118
 * @version 1.0
 * @description: TODO
 * @date 2025/5/12 11:15
 */
//定义rabbitmq配置类
@Configuration
public class RabbitConfig {
    //定义交换机名称
    public static final String CAR_NOTICE_EXCHANGE = "car_notice_exchange";
    //定义商家队列名称
    public static final String CAR_SHOP_NOTICE_QUEUE = "car_shop_queue";
    //定义用户队列名称
    public static final String CAR_USER_NOTICE_QUEUE = "car_user_queue";
    //定义全部用户队列名称
    public static final String CAR_ALL_NOTICE_QUEUE = "car_all_queue";
    //定义删除信息队列名称
    public static final String CAR_DELETE_NOTICE_QUEUE = "car_delete_queue";
    //定义订单消息队列名称
    public static final String CAR_ORDER_MESSAGE_QUEUE = "order_message_queue";
    //定义救援消息队列名称
    public static final String CAR_RESCUE_MESSAGE_QUEUE = "rescue_message_queue";
    //定义救援消息等待队列名称
    public static final String CAR_RESCUE_MESSAGE_WAIT_QUEUE = "rescue_message_wait_queue";
    //定义救援消息死信队列名称
    public static final String CAR_RESCUE_MESSAGE_DEAD_QUEUE = "rescue_message_dead_queue";
    //定义提现消息队列名称
    public static final String CAR_WITHDRAWAL_MESSAGE_QUEUE = "withdrawal_message_queue";
    //定义体现消息死信队列名称
    public static final String CAR_WITHDRAWAL_MESSAGE_DEAD_QUEUE = "withdrawal_message_dead_queue";
    //定义删除救援消息队列名称
    public static final String CAR_DELETE_RESCUE_MESSAGE_QUEUE = "delete_rescue_message_queue";


    @Bean
    public Queue carDeleteRescueMessageQueue() {
        return QueueBuilder.durable(CAR_DELETE_RESCUE_MESSAGE_QUEUE)
            .build();
    }
    @Bean
    public Binding carDeleteRescueMessageBinding() {
        return BindingBuilder.bind(carDeleteRescueMessageQueue()).to(carNoticeExchange()).with("deleteRescue");
    }

    @Bean   // 消息转换器，定义在配置文件中
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    //声明交换机
    @Bean
    public DirectExchange carNoticeExchange() {
        return ExchangeBuilder.directExchange(CAR_NOTICE_EXCHANGE) // 交换机名称
                .durable(true) // 持久化交换机
                .build();
    }

    @Bean
    public Queue carWithdrawalMessageQueue() {
        return QueueBuilder.durable(CAR_WITHDRAWAL_MESSAGE_QUEUE)
                .ttl(1000*60) //一分钟后过期
               .deadLetterExchange(CAR_NOTICE_EXCHANGE) // 死信交换机
               .deadLetterRoutingKey("withdrawalDead") // 死信路由键
               .build();
    }

    @Bean
    public Binding carWithdrawalMessageBinding() {
        return BindingBuilder.bind(carWithdrawalMessageQueue()).to(carNoticeExchange()).with("withdrawal");
    }
    @Bean
    public Queue carWithdrawalMessageDeadQueue() {
        return QueueBuilder.durable(CAR_WITHDRAWAL_MESSAGE_DEAD_QUEUE)
              .build();
    }

    @Bean
    public Binding carWithdrawalMessageDeadBinding() {
        return BindingBuilder.bind(carWithdrawalMessageDeadQueue()).to(carNoticeExchange()).with("withdrawalDead");
    }

    @Bean
    public Queue carRescueMessageWaitQueue() {
        return QueueBuilder.durable(CAR_RESCUE_MESSAGE_WAIT_QUEUE)
                .ttl(1000*60) //一分钟后过期
                .deadLetterExchange(CAR_NOTICE_EXCHANGE) // 死信交换机
                .deadLetterRoutingKey("rescueDead") // 死信路由键
                .build();
    }
    @Bean
    public Binding carRescueMessageWaitBinding() {
        return BindingBuilder.bind(carRescueMessageWaitQueue()).to(carNoticeExchange()).with("rescueWait");
    }
    @Bean
    public Queue carRescueMessageDeadQueue() {
        return QueueBuilder.durable(CAR_RESCUE_MESSAGE_DEAD_QUEUE)
               .build();
    }
    @Bean
    public Binding carRescueMessageDeadBinding() {
        return BindingBuilder.bind(carRescueMessageDeadQueue()).to(carNoticeExchange()).with("rescueDead");
    }
    //声明队列
    @Bean
    public Queue carShopNoticeQueue() {
        return QueueBuilder.durable(CAR_SHOP_NOTICE_QUEUE).build();
    }
    @Bean
    public Queue carDeleteNoticeQueue() {
        return QueueBuilder.durable(CAR_DELETE_NOTICE_QUEUE).build();
    }
    @Bean
    public Binding carDeleteNoticeBinding() {
        return BindingBuilder.bind(carDeleteNoticeQueue()).to(carNoticeExchange()).with("delete");
    }
    @Bean
    public Queue carUserNoticeQueue() {
        return QueueBuilder.durable(CAR_USER_NOTICE_QUEUE).build();
    }
    @Bean
    public Queue carAllNoticeQueue() {
        return QueueBuilder.durable(CAR_ALL_NOTICE_QUEUE).build();
    }
    @Bean
    public Queue carOrderMessageQueue() {
        return QueueBuilder.durable(CAR_ORDER_MESSAGE_QUEUE).build();
    }
    @Bean
    public Queue carRescueMessageQueue() {
        return QueueBuilder.durable(CAR_RESCUE_MESSAGE_QUEUE).build();
    }
    //绑定队列到交换机
    @Bean
    public Binding carRescueMessageBinding() {
        return BindingBuilder.bind(carRescueMessageQueue()).to(carNoticeExchange()).with("rescue");
    }
    @Bean
    public Binding carOrderMessageBinding() {
        return BindingBuilder.bind(carOrderMessageQueue()).to(carNoticeExchange()).with("order");
    }
    //绑定队列到交换机
    @Bean
    public Binding carShopNoticeBinding() {
        return BindingBuilder.bind(carShopNoticeQueue()).to(carNoticeExchange()).with("shop");
    }
    @Bean
    public Binding carUserNoticeBinding() {
        return BindingBuilder.bind(carUserNoticeQueue()).to(carNoticeExchange()).with("user");
    }
    @Bean
    public Binding carAllNoticeBinding() {
        return BindingBuilder.bind(carAllNoticeQueue()).to(carNoticeExchange()).with("all");
    }


}
